{-# LANGUAGE TupleSections, OverloadedStrings #-}
module Handler.Home where

import Import
import System.Cmd (rawSystem)

-- ######################### Handlers ######################### --

-- This is a handler function for the GET request method on the HomeR
-- resource pattern. All of your resource patterns are defined in
-- config/routes
--
-- The majority of the code you will write in Yesod lives in these handler
-- functions. You can spread them across multiple files if you are so
-- inclined, or create a single monolithic file.

getHomeR :: Handler RepHtml
getHomeR = do
  sess <- lookupSession "Authed"
  case sess of
    Just _  -> do
      (homeWidget, homeEnctype) <- generateFormPost moulinetteForm
      defaultLayout $ do
        setTitle "SDDPV1"
        $(widgetFile "homepage")
    Nothing ->redirect AutR

postHomeR :: Handler RepHtml
postHomeR = do
  ((resultHome, homeWidget), homeEnctype) <- runFormPost moulinetteForm
  (_) <- case resultHome of
    FormSuccess res -> do
      (_) <- liftIO $ rawSystem "echo" ["lol"]
      (_) <- sendFile typePng "/home/sarfraz/Images/mutt.png"
      redirect HomeR
    _               -> redirect HomeR

  defaultLayout $ do
    setTitle "SDDPV1"
    $(widgetFile "homepage")

getAutR :: Handler RepHtml
getAutR = do
  ((resultAutForm, autFormWidget), autFormEnctype) <- runFormPost autForm
  defaultLayout $ do
    setTitle "Please authentificate"
    $(widgetFile "autform")

postAutR :: Handler RepHtml
postAutR = do
  ((resultAutForm, autFormWidget), autFormEnctype) <- runFormPost autForm
  (_) <- case resultAutForm of
    FormSuccess res -> do
      checkAndDirect (fst res) (snd res)
    _               -> redirect AutR
  defaultLayout $ do
    setTitle "Please authentificate"
    $(widgetFile "autform")


-- ######################### Processes ######################### --

checkAndDirect name passw = do
  curUsed <- runDB $ getBy $ UniqueName name
  case curUsed of
    Just (Entity _ res) -> if ((usedPassword res) == passw) && (usedStatus res)
                then do
                  setSession "Authed" "yes"
                  redirect HomeR
                else redirect AutR
    _        -> redirect AutR

-- ######################### Forms ######################### --

data Version = Version1 | Version2 | Version3 | Experimental
             deriving (Show, Eq, Enum, Bounded)

checkVersion :: Version -> Int
checkVersion v =
  case v of
    Version1 -> 1
    Version2 -> 2
    Version3 -> 3
    _        -> 0

moulinetteForm :: Form (Version, FileInfo)
moulinetteForm =
  renderDivs $ (,)
  <$> areq (selectFieldList versions) "" Nothing
  <*> fileAFormReq ""
  where
    versions :: [(Text, Version)]
    versions = [("Version 1", Version1), ("Version 2", Version2), ("Version 3", Version3)]

autForm :: Form (Text, Text)
autForm =
  renderDivs $ (,)
  <$> areq textField "Name"     Nothing
  <*> areq textField "Password" Nothing
